package com.medicine.hbp.ui;


import org.kymjs.kjframe.ui.BindView;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.medicine.hbp.base.BaseActivity;
import com.medicine.hbp.kankan.OnWheelChangedListener;
import com.medicine.hbp.kankan.WheelView;
import com.medicine.hbp.kankan.adapters.ArrayWheelAdapter;
import com.medicine.hbp.uitl.ToastUtil;
import com.medicine.hbp.R;

public class UserSettingSelectActivity extends BaseActivity implements OnWheelChangedListener {
	@BindView(id=R.id.set_edit)public LinearLayout set_edit;
	@BindView(id=R.id.set_radio)public LinearLayout set_radio;
	@BindView(id=R.id.ll_sds)public LinearLayout set_sds;


	@BindView(id=R.id.txt_set_name)public TextView txt_set_name;
	@BindView(id=R.id.edit_set_name)public EditText edit_set_name;

	@BindView(id=R.id.group_set)public RadioGroup group_set;
	@BindView(id=R.id.group_nan)public RadioButton group_nan;
	@BindView(id=R.id.group_nv)public RadioButton group_nv;

	@BindView(id=R.id.id_province)public WheelView mViewProvince;
	@BindView(id=R.id.id_city)public WheelView mViewCity;
	@BindView(id=R.id.id_district)public WheelView mViewDistrict;


	private String  sex="";//Y 男 N 女
	private int type;//1 昵称  2 签名 3 性别  4 地址
	@Override
	public void setRootView() {
		super.setRootView();
		type=getIntent().getIntExtra("type", 0);
		setContentView(R.layout.activity_usersettingselect);
	}

	@Override
	public void initWidget() {
		super.initWidget();
		if(type==1){
			txt_set_name.setText("昵称");
			set_edit.setVisibility(View.VISIBLE);
			set_radio.setVisibility(View.GONE);
			set_sds.setVisibility(View.GONE);
		}else if(type==2){
			txt_set_name.setText("签名");
			set_edit.setVisibility(View.VISIBLE);
			set_radio.setVisibility(View.GONE);
			set_sds.setVisibility(View.GONE);
		}else if(type==3){

			set_edit.setVisibility(View.GONE);
			set_radio.setVisibility(View.VISIBLE);
			set_sds.setVisibility(View.GONE);
		}else if(type==4){
			set_edit.setVisibility(View.GONE);
			set_radio.setVisibility(View.GONE);
			set_sds.setVisibility(View.VISIBLE);
		}
		group_set.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup arg0, int arg1) {
				switch (arg1) {
					case R.id.group_nan:
						sex="Y";
						break;
					case R.id.group_nv:
						sex="N";
						break;
				}
			}
		});
		// 添加change事件
		mViewProvince.addChangingListener(this);
		// 添加change事件
		mViewCity.addChangingListener(this);
		// 添加change事件
		mViewDistrict.addChangingListener(this);
		initProvinceDatas();
		mViewProvince.setViewAdapter(new ArrayWheelAdapter<String>(UserSettingSelectActivity.this, mProvinceDatas));
		// 设置可见条目数量
		mViewProvince.setVisibleItems(7);
		mViewCity.setVisibleItems(7);
		mViewDistrict.setVisibleItems(7);
		updateCities();
		updateAreas();
		initTop();
	}

	@Override
	public void widgetClick(View v) {
		super.widgetClick(v);
	}
	private void initTop() {
		TopView topview = (TopView) findViewById(R.id.topview);
		topview.setTitle("信息设置");
		topview.setLeftVisibility(View.GONE);
		topview.setRightTxtVisibility(View.VISIBLE);
		topview.setLeftVisibilityExit(View.VISIBLE);
		topview.setRightVisibility(View.GONE);
		topview.setLeftOnClickExit(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		topview.setTextRightOnClick(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if(type==1){
					if(TextUtils.isEmpty(edit_set_name.getText().toString())){
						ToastUtil.showShort(aty, "还没填写昵称");
						return;
					}
					Intent intent=new Intent();
					intent.putExtra("str", edit_set_name.getText().toString());
					setResult(RESULT_OK, intent);
					finish();
				}else if(type==2){
					if(TextUtils.isEmpty(edit_set_name.getText().toString())){
						ToastUtil.showShort(aty, "还没填写签名");
						return;
					}
					Intent intent=new Intent();
					intent.putExtra("str", edit_set_name.getText().toString());
					setResult(RESULT_OK, intent);
					finish();
				}else if(type==3){
					if(TextUtils.isEmpty(sex)){
						ToastUtil.showShort(aty, "还没选择性别");
						return;
					}
					Intent intent=new Intent();
					intent.putExtra("str", sex);
					setResult(RESULT_OK, intent);
					finish();
				}else if(type==4){
					Intent intent=new Intent();
					intent.putExtra("Provice", mCurrentProviceName);
					intent.putExtra("City", mCurrentCityName);
					intent.putExtra("District", mCurrentDistrictName);
					setResult(RESULT_OK, intent);
					finish();
				}
			}
		});
	}

	@Override
	public void onChanged(WheelView wheel, int oldValue, int newValue) {
		// TODO Auto-generated method stub
		if (wheel == mViewProvince) {
			updateCities();
		} else if (wheel == mViewCity) {
			updateAreas();
		} else if (wheel == mViewDistrict) {
			mCurrentDistrictName = mDistrictDatasMap.get(mCurrentCityName)[newValue];
			mCurrentZipCode = mZipcodeDatasMap.get(mCurrentDistrictName);
		}
	}
	/**
	 * 根据当前的省，更新市WheelView的信息
	 */
	private void updateCities() {
		int pCurrent = mViewProvince.getCurrentItem();
		mCurrentProviceName = mProvinceDatas[pCurrent];
		String[] cities = mCitisDatasMap.get(mCurrentProviceName);
		if (cities == null) {
			cities = new String[] { "" };
		}
		mViewCity.setViewAdapter(new ArrayWheelAdapter<String>(this, cities));
		mViewCity.setCurrentItem(0);
		updateAreas();
	}
	/**
	 * 根据当前的市，更新区WheelView的信息
	 */
	private void updateAreas() {
		int pCurrent = mViewCity.getCurrentItem();
		mCurrentCityName = mCitisDatasMap.get(mCurrentProviceName)[pCurrent];
		String[] areas = mDistrictDatasMap.get(mCurrentCityName);

		if (areas == null) {
			areas = new String[] { "" };
		}
		mViewDistrict.setViewAdapter(new ArrayWheelAdapter<String>(this, areas));
		mViewDistrict.setCurrentItem(0);
	}
}
